package com.web.servlet;

import com.dao.EarthDao;
import com.web.common.exception.DataAccessException;
import com.web.common.utils.BeanFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author wangzhipeng
 * @Date 2020-08-31 11:57 上午
 * @Description
 */
@WebServlet(urlPatterns = "/GetNewQuakeServlet")
public class GetNewQuakeServlet extends HttpServlet {
    private EarthDao earthDao= (EarthDao) BeanFactory.getBean(BeanFactory.EARTH_DAO);
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String earthQuakeInfo = null;
        try {
             earthQuakeInfo = earthDao.getEarthquakeInfo();
        } catch (Exception e) {
            e.printStackTrace();
        }
        response.setContentType("application/json;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        PrintWriter printWriter = response.getWriter();
        printWriter.println(earthQuakeInfo);
        printWriter.flush();
        printWriter.close();
    }
}
